@layout($syslayout)

@section('content')
<div class="page-header">
     <h3>{{ Str::title($flow) }} > {{ HTML::link('admin/modul/rule/'.URI::segment(5), 'Rule') }}</h3>
</div>
<div class="row-fluid">
  <a href="#addStepModal" role="button" class="btn pull-right" data-toggle="modal" style='margin-bottom:10px'><i class="icon-plus"></i>&nbsp;New Step</a>
</div>
<div class="row-fluid">
  <div class="span12 pull-left">
    <div id="steplist" class="row-fluid" style="overflow:auto">
      {{ $steplist }}
    </div>
  </div>
</div>

<!-- MODAL -->
<div id="addStepModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Add New Step / Status</h3>
  </div>
  <div class="modal-body">
      {{ Form::open('admin/user/role', 'POST', array('id' => 'addStepForm', 'class' => 'form-horizontal')) }}
      {{ Form::hidden('sf_id',URI::segment(5)) }}
      {{ Form::hidden('sfs_id') }}
      {{ Form::control_group(Form::label('sfs_step', 'Step / Status'),Form::xlarge_text('sfs_step',null,array('placeholder'=>'Type Data Value','required')));}}
      {{ Form::close()}}
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button id="addStepBtn" class="btn btn-primary">Save</button>
  </div>
</div>

@endsection

@section('scripts')
<script>

  $('#addStepBtn').click(function() {

      $.post('{{ url('admin/modul/step'); }}', $("#addStepForm").serialize(),function(data) {
              sourcedata = data;
            }).success(function() { 
              sourcedata = jQuery.parseJSON(sourcedata);
              $("#steplist" ).empty().html( sourcedata );
              $("#addStepForm :input[name='sfs_step']").val('');
              datasource();
              $('#addStepModal').modal('hide');
            });
    
  });

  function editStep(id){

      $('#addStepModal').modal('toggle');
      $('#myModalLabel').empty().html('Edit Step')

      $.get('{{ url('admin/modul/stepinfo'); }}', { stepid: id},function(data,status){

        for (x in data)
        {   
          $('#addStepForm input[name="'+ x +'"]' ).val(data[x]);
        }

      },"json");

  }


  function deleteStep(id,name){

    $.post('{{ url('admin/modul/deletestep'); }}', "step="+name+"&id="+id+"&flowid="+{{ URI::segment(5) }} ,function(data) {
          sourcedata = data;
        }).success(function() {
            sourcedata = jQuery.parseJSON(sourcedata);
            $( "#steplist" ).empty().append( sourcedata );
            datasource();
          });
  }


  function datasource(){

      $.get('{{ url('admin/modul/resetnavdata'); }}',  function(data,status){

      for (x in data)
      {   

        var datavalue = data[x];
        var $el = $("[name=" + x + "]");
        $el.empty();

        $.each(datavalue, function(key, value) {
          $el.append($("<option></option>").attr("value", key).text(value));

        });

      }

      },"json");

  }

</script>
@endsection